//
//  DKTabBarController.m
//  BTApp
//
//  Created by 丁侃 on 16/4/16.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "DKTabBarController.h"
#import "DKStatusViewController.h"
#import "DKMessageViewController.h"
#import "DKHomeViewController.h"
#import "DKDiscoveryViewController.h"
#import "DKCanmeraViewController.h"

#import "DKNormalNavigationController.h"
#import "DKTabBar.h"

@interface DKTabBarController()<DKTabBarDelegate>

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation DKTabBarController

-(NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    
    //添加子控制器
    [self addChildViewControllers];
    
    //自定义tabBar
    [self addTabBar];
}

//添加自定义的tabBar
-(void)addTabBar
{
    
    DKTabBar *tabBar = [[DKTabBar alloc]initWithFrame:self.tabBar.bounds];
    
    tabBar.items = self.items;
    
    tabBar.delegate = self;
    
    [self.tabBar addSubview:tabBar];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //移除系统tabBar上的按钮
    for (UIView *subV in self.tabBar.subviews) {
        if (![subV isKindOfClass:[DKTabBar class]]) {
            [subV removeFromSuperview];
        }
    }
}

#pragma mark - DKTabBarDelegate
-(void)tabBar:(DKTabBar *)tabBAr btnDidClick:(UIButton *)btn
{
    self.selectedIndex = btn.tag;
}



//添加子控制器
-(void)addChildViewControllers
{
    
    //添加主页Home界面
    DKHomeViewController *home = [[DKHomeViewController alloc]init];
    [self addOneChildVCWithRootVc:home withImage:[UIImage imageNamed:@"tab_首页"] withSelectedImage:[UIImage imageNamed:@"tab_首页_pressed"] withNavigationItemTitle:nil];

    //添加发现界面
    DKDiscoveryViewController *dicovery = [[DKDiscoveryViewController alloc]init];
    dicovery.view.backgroundColor = [UIColor whiteColor];
    [self addOneChildVCWithRootVc:dicovery withImage:[UIImage imageNamed:@"tab_社区"] withSelectedImage:[UIImage imageNamed:@"tab_社区_pressed"] withNavigationItemTitle:@"discovery"];
    
    //添加照相界面
    DKCanmeraViewController *canmera = [[DKCanmeraViewController alloc]init];
    canmera.view.backgroundColor = [UIColor blueColor];
    [self addOneChildVCWithRootVc:canmera withImage:[UIImage imageNamed:@"tab_好物"] withSelectedImage:[UIImage imageNamed:@"tab_好物_pressed"] withNavigationItemTitle:@"canmera"];
    
    //添加收信界面
    DKMessageViewController *message = [[DKMessageViewController alloc]init];
    message.view.backgroundColor = [UIColor greenColor];
    [self addOneChildVCWithRootVc:message withImage:[UIImage imageNamed:@"tab_分类"] withSelectedImage:[UIImage imageNamed:@"tab_分类_pressed"] withNavigationItemTitle:@"消息"];
    
    //添加个人界面
    DKStatusViewController *status = [[DKStatusViewController alloc]init];
    status.view.backgroundColor = [UIColor yellowColor];
    [self addOneChildVCWithRootVc:status withImage:[UIImage imageNamed:@"tab_我的"] withSelectedImage:[UIImage imageNamed:@"tab_我的_pressed"] withNavigationItemTitle:@"status"];
    

}

//添加一个自控制器
-(void)addOneChildVCWithRootVc:(UIViewController *)vc withImage:(UIImage *)image withSelectedImage:(UIImage *)selImage withNavigationItemTitle:(NSString *)title
{
    
    UINavigationController *nav = nil;
    
    if ([vc isKindOfClass:[DKHomeViewController class]]) {
        
        nav = [[UINavigationController alloc]initWithRootViewController:vc];
        
    }else{
    
        nav = [[DKNormalNavigationController alloc]initWithRootViewController:vc];
    
    }
    
    
    
    
    
    
    
    vc.navigationItem.title = title;

    [nav.tabBarItem setImage:[UIImage imageWihtRenderingImage:image]];
    [nav.tabBarItem setSelectedImage:[UIImage imageWihtRenderingImage:selImage]];
    
    [self.items addObject:nav.tabBarItem];
    
    
    [self addChildViewController:nav];
}



@end
